package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.adapter2.Haoyouyaoqinadapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.Invite;
import com.huifaapp.mall.bean.Huishengqian.Share;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Umshares;
import com.huifaapp.mall.view.Wuview.RecyclerViewDivider;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.List;

public class FenxiangActivity extends AppCompatActivity {
    private PopupWindow window;
    private  int startint =0;
    private  int lenghtint = 2;
    private Button button;
    private XRecyclerView xyhaooyu;
    private Share.DataBean data;
    private RelativeLayout relativeLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fenxiang);
        initview();

        getNetyaoPles(startint,lenghtint);
        getNetlianjie();
        relativeLayout = (RelativeLayout) findViewById(R.id.re_haoyou);
        findViewById(R.id.Gvmine_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            finish();
            }
        });

        findViewById(R.id.dianwo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //分享
                View view1 =   LayoutInflater.from(FenxiangActivity.this).inflate(R.layout.popwindownshangtan,null, false);
                window = new PopupWindow(view1, RelativeLayout.LayoutParams.MATCH_PARENT,600,true );
                window.setOutsideTouchable(true);
                window.setTouchable(true);
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.alpha = 0.7f;
                getWindow().setAttributes(lp);
                window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams lp = getWindow().getAttributes();
                        lp.alpha = 1f;
                        getWindow().setAttributes(lp);
                    }
                });
                /*
                        分享朋友
                 */
                view1.findViewById(R.id.share_penyou).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Umshares.shareWeb(FenxiangActivity.this,data.getUrl(),data.getTitle(),
                                data.getContent(),data.getImgUrl(),R.drawable.guanbi, SHARE_MEDIA.WEIXIN);

                    }
                });
                /*
                        分享朋友圈
                 */
                view1.findViewById(R.id.share_pengyouquna).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Umshares.shareWeb(FenxiangActivity.this,data.getUrl(),data.getTitle(),
                                data.getContent(),data.getImgUrl(),R.drawable.guanbi, SHARE_MEDIA.WEIXIN_CIRCLE);

                    }
                });
                //关闭弹窗
                view1.findViewById(R.id.imgguanbi).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                    }
                });
                window.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM , 0, 0);


            }
        });

    }
        /*
                请求连接

         */
    private void getNetlianjie() {
        String url = ApiManager.Share + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
        Log.e("yaoqinghaoyou", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {



            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Share share = new Gson().fromJson(responseInfo.result, Share.class);
                if (share.getCode().equals("10000")){
                    data = share.getData();

                }else {
                    Toast.makeText(FenxiangActivity.this,share.getMsg(),Toast.LENGTH_SHORT).show();
                }



            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(FenxiangActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });



    }

    private void initview() {
        button = (Button) findViewById(R.id.but_yaoqinghaoyou);
        xyhaooyu = (XRecyclerView) findViewById(R.id.xhayou);
        xyhaooyu.setPullRefreshEnabled(true);
        xyhaooyu.setLoadingMoreEnabled(true);
        //添加分割线
        xyhaooyu.addItemDecoration(new RecyclerViewDivider(FenxiangActivity.this, LinearLayoutManager.HORIZONTAL));
        LinearLayoutManager manager = new LinearLayoutManager(FenxiangActivity.this,LinearLayoutManager.VERTICAL,false);
        xyhaooyu.setLayoutManager(manager);

    }

    private void getNetyaoPles(int startint, int lenghtint) {
        String url = ApiManager.INvite + "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this)+"&start="+startint+"&length="+lenghtint;
        Log.e("yaoqinghaoyou", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Invite invite = new Gson().fromJson(responseInfo.result, Invite.class);
              if (invite.getCode().equals("10000")){
                  Invite.DataBean data = invite.getData();
                  button.setText("成功邀请"+data.getTotal()+"位好友");
                  //判断是否有好友

                          if (data.getTotal()==0){
                              relativeLayout.setVisibility(View.GONE);
                  }else {
                              relativeLayout.setVisibility(View.VISIBLE);
                  }

                  List<Invite.DataBean.ListBean> list = data.getList();
                  xyhaooyu.setAdapter(new Haoyouyaoqinadapter(FenxiangActivity.this,list));

              }else {
                  Toast.makeText(FenxiangActivity.this,invite.getMsg(),Toast.LENGTH_SHORT).show();
              }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(FenxiangActivity.this,s,Toast.LENGTH_SHORT).show();

            }
        });
    }
}
